package com.zkj.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkj.constant.JwtClaimsConstant;
import com.zkj.constant.MessageConstant;
import com.zkj.dto.UserLoginDTO;
import com.zkj.entity.User;
import com.zkj.exception.EmployeeNotLoginException;
import com.zkj.properties.JwtProperties;
import com.zkj.properties.WechatProperties;
import com.zkj.result.Result;
import com.zkj.service.IUserService;
import com.zkj.utils.HttpClientUtil;
import com.zkj.utils.JwtUtil;
import com.zkj.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: zhangKaiJun
 * @createTime：2024/9/5
 * @description：
 */
@RestController
@Api(tags = "用户模块")
@Slf4j
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    // 登录
    @ApiOperation("登录")
    @PostMapping("/login")
    @CrossOrigin
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {

        User user = userService.login(userLoginDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(), claims);

        UserLoginVO userLoginVO =
                UserLoginVO.builder()
                        .openid(user.getOpenid())
                        .token(jwt).id(user.getId())
                        .deliveryFee(0)
                        .shopName("毛卓瑶零食店")
                        .shopAddress("太原市")
                        .shopId("11")
                        .build();
        log.info("登录成功:{}", JSONObject.toJSONString(userLoginVO));
        return Result.success(userLoginVO);

    }


}
